package org.jeecg.common.util;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CodeUtil {

    public static <T> List<Collection> splitList(Collection<T> collection, int maxSize, int splitSize) {
        if (CollectionUtils.isEmpty(collection)) {
            return Collections.emptyList();
        }
        return Stream.iterate(0, f -> f + 1)
                .limit(maxSize)
                .parallel()
                .map(a -> collection.parallelStream().skip(a * splitSize).limit(splitSize).collect(Collectors.toList()))
                .filter(b -> !b.isEmpty())
                .collect(Collectors.toList());
    }


    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>(1001);
        for (int i = 0; i < 950; i++) {
            list.add(i);
        }
        int maxSize = list.size() / 100 + 1;
        List<Collection> splitList = splitList(list, maxSize, 100);
        System.out.println("共分" + maxSize + "次");
        for (int i = 0; i < maxSize; i++) {
            splitList.get(i).forEach(e -> {
                System.out.println(e);
            });
        }
    }

}
